package com.sxj.corejava.code08_常用类.Object类;

import java.util.Objects;

/**
 * @author 石小俊
 * @date 2025年01月10日
 */
public class Person implements Cloneable {
    private String name;
    private int age;
    private Address address;

    public Person(String name, int age, Address address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
// 可以手动重写equals方法
//    @Override
//    public boolean equals(Object obj) {
//        // 1.参数对象与当前方法调用者必须是同一个对象
//        if (obj.getClass() == Person.class) {
//            // 转换成对应对象
//            Person person = (Person) obj;
//            // 2.当name的值与age的值相等时表示对象相等
//            return this.name.equals(person.getName()) && this.age == person.getAge();
//        }
//        // 如果不是同一个对象,则直接返回false
//        return false;
//    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

//    @Override
//    public String toString() {
//        // 在toString方法中,返回值即为打印对象时展示的信息
////        return "这是重写的toString方法";
//        return "Person{name=" + name + ",age=" + age + "}";
//    }


//    @Override
//    public String toString() {
//        return "Person{" +
//                "name='" + name + '\'' +
//                ", age=" + age +
//                '}';
//    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("gc正在回收垃圾");
        super.finalize();
    }

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
